package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.ActivityDAO;
import com.zerosky.crm.po.Activity;
import com.zerosky.crm.service.ActivityService;
import com.zerosky.crm.util.PageInfo;

/**
 * Activity业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="activityService")
public class ActivityServiceImpl implements ActivityService {

	@Autowired
	private ActivityDAO activityDAO; // Activity数据接口

	/**
	 * Activity列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) //不开启事物
	public List<Activity> getActivityList(Map map) {
			return activityDAO.getActivityList(map);
	}

	/**
	 * 通过主键查找Activity实体
	 * 
	 * @param id
	 *            Activity主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) //不开启事务
	public Activity getActivityById(String id) {
			return activityDAO.getActivityById(id);
	}

	/**
	 * 保存Activity
	 * 
	 * @param Activity
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation=Propagation.REQUIRED) //创建一个事务
	public void saveActivity(Activity activity, String operation) throws Exception {
		if ("新增".equalsIgnoreCase(operation)) {
				activityDAO.addActivity(activity);
		} else if ("修改".equalsIgnoreCase(operation)) {
				activityDAO.updateActivity(activity);
		}
	}

	/**
	 * 批量删除Activity
	 * 
	 * @param idArray
	 *            Activity主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation=Propagation.REQUIRED) //创建一个事务
	public void deleteActivityById(String id) throws Exception {
			activityDAO.deleteActivityById(id);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) //不开启事务
	public PageInfo<Activity> getPageInfoActivityList(Map map, PageInfo<Activity> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<Activity> list =  activityDAO.getActivityList(map);
		page.setList(list);

		int count = activityDAO.countActivity(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}
}
